package com.demo.swingquickticket.swing.model;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Data
@EqualsAndHashCode(callSuper = true)
public class AppJTableModel extends AbstractTableModel {

    List<String> header = new ArrayList<>();

    List<List<Object>> data = new ArrayList<>();

    List<List<Object>> searchData = new ArrayList<>();

    List<Class> colType = new ArrayList<>();

    String fromStation;

    String toStation;

    String startDate;

    @Override
    public int getRowCount() {
        return getSearchData().size();
    }

    @Override
    public int getColumnCount() {
        return getHeader().size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return getSearchData().get(rowIndex).get(columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return getHeader().get(column);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0;
    }

    @Override
    public void setValueAt(Object newValue, int rowIndex, int columnIndex) {
        getSearchData().get(rowIndex).set(columnIndex, newValue);
    }

    public void setData(List<List<Object>> data) {
        setData(data, null);
    }

    public void setData(List<List<Object>> data, Set<String> searchType) {
        this.data = data;
        setSearchData(searchType);
    }


    public void setSearchData(Set<String> searchType) {
        this.searchData.clear();
        if (searchType == null || searchType.contains("All")) {
            this.searchData.addAll(this.data);
        } else {
            for (List<Object> temp : this.data)
            if (searchType.contains(String.valueOf(temp.get(1)).substring(0,1).toUpperCase())) {
                this.searchData.add(temp);
            }
        }
    }

    public List<String> getSelectedStationCode() {
        List<String> stationList = new ArrayList<>();
        for (List<Object> row : searchData) {
            if ((boolean)row.get(0)) {
                stationList.add((String) row.get(1));
            }
        }
        return stationList;
    }
}
